package com.sky.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.sky.exception.BaseException;
import com.sky.properties.AliPayProperties;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * 支付工具类
 *
 * @Author wd
 * @Date 2025/5/26 21:44
 */
@Component
@Slf4j
public class AliPayUtil {

//    @Autowired
//    private static AliPayProperties serAlipayConfig;


    public static String payment(BigDecimal amount, String subject, String orderNo, String userId) throws AlipayApiException {
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCSwGK4W9SU8QN62U7yj9ke9u9aTMO3VdrL9C5sB6FuhDEoyw3L/xzmM02ghxDUv7MxhtJHTga5GBXL2DoSaldo2LJqjduikJAXCFv9Pn6VjnjNQlW1ZyCt0CpllzsuqBKFm6RLf69f0LB8hMK0S04TnGY/GYFmt6w1cGzpkrev0k5008YWdz6938/Z8sjv1/6+rJROhqS8/UxMOLmDeuB2LR7/sZErK8rjOtG+37mIiPPKuV/MZGrgmj2cSHTVfNR72Dbq5EOY3+Q3m7Y5PdrltudSCw3dJvU2vMqfg4btVM5YmBAyWXAMZbBjByAkZwJ94NGcuJobU3vBF58usFZhAgMBAAECggEAIo/y8C6Yi5eT1TwW7uPYJAXOa3sG4Uf0JvLUeBb3+zphBZWFI1VrLgBJORR2b1TS1EhHgAmtZax5fa/oRWwMOCJJKvVMFCZt+S6705IDaB/M7bLA0t99/9G2GoUaqCtnaVY1xJ5G5I8W8cymuqW9trqwDg5ovpNlmJRo4j3uyxakiXbg44ppnQQmWt/0kWD+7XF1Gl68YoOwxxlD0nkfQsLU9on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zjIX4137CRKU8BFqAVYfnWgGLLmIL0LfbgJKDX+QWtoqLOFFZ5dAMVSAgB1aDQ5H1cGD3pLtjbSub9dicleHBmIIxXN5766smqpVxA4FuZuGnwERfQQGaEUPlJsbz70rMrI6i/8PMy0P/NEHOavJI1nIn2sMAdW1uAZYAbGTiAT/4ovr18+Y9uumymcsYWyGiQRAHSzorVmVjQk/yN5P9LbIoGY2BiFpUKWzGZ133BMbvwTAFwT6h8tSjkYKRCX5D4MT0+22NqY/mcNZMvh/ctHQr3Oj+PObkZxvmW4zn7YjGthCnCEgTj389bOHYRwp/b0EAg2/wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000149604048");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setBody(userId);
        model.setOutTradeNo(orderNo);
        model.setTotalAmount(String.valueOf(amount));
        model.setSubject(subject);
        model.setProductCode("QUICK_WAP_WAY");
        model.setSellerId("2088102147948060");
        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            return pageRedirectionData;
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return null;
    }


}
